package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService{
    @Autowired
    private MemberDao memberDao;



    @Override
    public Member getMemberByPhone(String telephone) {
        Member member=memberDao.getMemberByPhone(telephone);
        return member;
    }

    @Override
    public void regMember(Member member) {
        memberDao.regMember(member);
    }

    @Override
    public void add(Member member) {
        memberDao.add(member);
    }

    @Override//months,memberCount
    public Map getMemberReport() {
        //获取日期，不同日期时间对应的会员人数
        //获取日历空间实例对象
        Calendar calendar = Calendar.getInstance();
        //将时间向前推12个月
        calendar.add(Calendar.MONTH,-12);
        //创建集合存储对应的月数
        List<String> list = new ArrayList<>();
        //创建集合存储对应月数前的会员数
        List<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);
            String date = new SimpleDateFormat("yyyy.MM").format(calendar.getTime());
            list.add(date);
            date=date+".31";//再数据库查询人数时，每次查询事件小于这个date的总人数
            Integer count=memberDao.getMemberByTime(date);
            arrayList.add(count);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("months",list);
        map.put("memberCount",arrayList);
        return map;
    }

    @Override
    public Integer gettodayNewMember(String today) {
        Integer count=memberDao.gettodayNewMember(today);
        return count;
    }

    @Override
    public Integer gettotalMember() {
        Integer count=memberDao.gettotalMember();
        return count;
    }

    @Override
    public Integer getNewMemberAfterDay(String date) {
        Integer count=memberDao.getNewMemberAfterDay(date);
        return count;
    }
}
